package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ActiveJoinDao;
import cn.ym.mall.dao.ActiveTypeDao;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.ActiveType;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 * 活动类别实现列
 */
public class ActiveTypeDaoImpl implements ActiveTypeDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加活动类型
     *
     * @param activeType
     * @return
     * @throws SQLException
     */
    @Override
    public long add(ActiveType activeType) throws SQLException {
        long num = 0;
        try{
            String sql = "insert into ym_activeType(actname) value(?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,activeType);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 删除活动类型
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public long delete(Integer aid) throws SQLException {
        long num = 0;
        try{
            String sql = "delete from ym_activeType where aid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,aid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 修改活动类型
     *
     * @param activeType
     * @return
     * @throws SQLException
     */
    @Override
    public long update(ActiveType activeType) throws SQLException {
        long num = 0;
        try{
            String sql = "update ym_activeType set actname=? where actid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,activeType);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有活动类型
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveType> getAll() throws SQLException {
        List<ActiveType> activeTypeList = new ArrayList<ActiveType>();
        try{
            String sql = "select * from ym_activeJoin;";
            activeTypeList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ActiveType>(ActiveType.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeTypeList;
    }

    /**
     * 根据id查询活动类型
     *
     * @param actid
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveType getActiveTypeById(Integer actid) throws SQLException {
        ActiveType activeType = null;
        try{
            String sql = "select * from ym_activeType where aid = ?;";
            activeType = queryRunner.query(sql,new BeanHandler<ActiveType>(ActiveType.class),actid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeType;
    }

    /**
     * 根据活动名查询活动类型
     *
     * @param actname
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveType getActiveTypeByType(String actname) throws SQLException {
        ActiveType activeType = null;
        try{
            String sql = "select * from ym_activeType where aid = '?';";
            activeType = queryRunner.query(sql,new BeanHandler<ActiveType>(ActiveType.class),actname);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeType;
    }

}
